#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes))

#include "TgcScene.h"

#include "Model.h"

#include "GraphicsLayer.h"

using namespace std;

cTgcScene::cTgcScene(char* filename) {
    this->InitScene(filename);
}

void cTgcScene::InitScene(char* filename) {
    this->m_xmlLoader = new cXmlLoader(filename);
    this->LoadProperties();
    delete this->m_xmlLoader;
    this->m_xmlLoader = NULL;
    this->CreateModels();
}

void cTgcScene::LoadProperties() {
    // Reservar el espacio necesario para los vectores    
    this->m_pModelData.reserve(m_xmlLoader->m_pModelData.size());
    this->m_pModelData.swap(m_xmlLoader->m_pModelData);
}

void cTgcScene::CreateModels()
{
    int length = this->m_pModelData.size();
    int i = 0;
    this->m_pModels.reserve(length);
    
    this->models_count = length;
    
    for (; i < length; i++)
    {
        std::string name = this->m_pModelData[i].name;
        int matID = this->m_pModelData[i].matID;
        
        this->m_pModels.push_back(
            new cModel(
                this->m_pModelData[i].m_pCordIdList,
                this->m_pModelData[i].m_pTextCoordsIdList,
                this->m_pModelData[i].m_pColorsIdList,
                this->m_pModelData[i].m_pMatIdsList,
                this->m_pModelData[i].m_pVertices,
                this->m_pModelData[i].m_pNormals,
                this->m_pModelData[i].m_pColors,
                this->m_pModelData[i].m_pTexCoords,
                name,
                matID
            )
        );
        
        this->m_pModelData[i].m_pCordIdList.clear();
        this->m_pModelData[i].m_pTextCoordsIdList.clear();
        this->m_pModelData[i].m_pColorsIdList.clear();
        this->m_pModelData[i].m_pMatIdsList.clear();
        this->m_pModelData[i].m_pVertices.clear();
        this->m_pModelData[i].m_pNormals.clear();
        this->m_pModelData[i].m_pColors.clear();
        this->m_pModelData[i].m_pTexCoords.clear();
    }
}

cModel* cTgcScene::getModel(int index)
{
    return this->m_pModels[index];
}

cTgcScene::~cTgcScene()
{
    int length = this->m_pModels.size();
    int i = 0;
    for (; i < length; i++) {
        delete this->m_pModels[i];
    }
    this->m_pModelData.clear();
}